package za.org.jcicada.genss.sensor.impl;

import za.org.jcicada.genss.sensor.ISensor;
import za.org.jcicada.genss.sensor.SensorStatus;

/**
 * Class used to apply measured sensor value objects to the sensor. Fills in a
 * generated message if none supplied if the status is not NOMINAL.
 * 
 * @author rcrida
 */
public class StatusRuleKatcp {

    /**
     * Set the value on the sensor if it already has a message, otherwise
     * generate a message.
     * 
     * @param sensor the sensor that the value is for
     * @param statusValue the value object composed from parameters received via katcp
     * @throws InvalidSensorValue if the sensor value was invalid for the sensor
     *             type
     */
    public static void setStatusValue(ISensor sensor, SensorStatusValue statusValue) throws InvalidSensorValue {
        if (statusValue.getMessage() != null) {
            sensor.setStatusValue(statusValue);
        } else {
            if (statusValue.getStatus() != SensorStatus.NOMINAL) {
                String message = String.format("The device set the sensor status to %s.", statusValue.getStatus().toString());
                sensor.setStatusValue(new SensorStatusValue(statusValue.getStatus(), statusValue.getTime(), message));
            }
        }
    }

}
